package com.car.account.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.car.account.dto.storesInfoDto.AdminSelectStoresInfoByIdDto;
import com.car.account.dto.storesInfoDto.AdminSelectStoresInfoDto;
import com.car.account.entity.Admin;
import com.car.account.entity.Stores;
import com.car.account.entity.StoresInfo;
import com.car.account.exception.AccountException;
import com.car.account.mapper.StoresInfoMapper;
import com.car.account.param.storesInfoParam.AdminSelectStoresInfoParam;
import com.car.account.service.AdminService;
import com.car.account.service.StoresInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.car.account.service.StoresService;
import com.car.account.util.PageDtoUtil;
import com.car.account.util.aliyunUtil.AliyunOssUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 董旭
 * @since 2024-08-28
 */
@Service
public class StoresInfoServiceImpl extends ServiceImpl<StoresInfoMapper, StoresInfo> implements StoresInfoService {

    @Resource
    private AdminService adminService;

    @Resource
    private StoresService storesService;

    @Resource
    private AliyunOssUtil aliyunOssUtil;

    @Override
    public AdminSelectStoresInfoByIdDto adminSelectStoresInfoDtoById(Integer adminId, Integer storesId) throws Exception {
        //判断是否存在平台账号
        Admin admin = adminService.getById(adminId);
        if (admin == null || admin.getAdminState() == 2) throw new AccountException(4001,"平台账号不存在");
        //判断平台账号状态
        if (admin.getAdminState() == 1) throw new AccountException(4002,"平台账号已被禁用");
        //判断是否存在门店信息
        StoresInfo storesInfo = getById(storesId);
        if (storesInfo == null || storesInfo.getStoresState() == 1) throw new AccountException(4003,"门店信息不存在");
        return BeanUtil.toBean(storesInfo, AdminSelectStoresInfoByIdDto.class);
    }

    @Override
    public Page<AdminSelectStoresInfoDto> adminSelectStoresInfoDto(AdminSelectStoresInfoParam param) throws Exception {
        //判断是否存在平台账号
        Admin admin = adminService.getById(param.getAdminId());
        if (admin == null || admin.getAdminState() == 2) throw new AccountException(4001,"平台账号不存在");
        //判断平台账号状态
        if (admin.getAdminState() == 1) throw new AccountException(4002,"平台账号已被禁用");
        //获取集合
        QueryWrapper<StoresInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("stores_name", param.getStoresName());
        Page<StoresInfo> page = new Page<>(param.getCurrent() == null ? 1 : param.getCurrent(), param.getSize() == null ? 6 : param.getSize());
        Page<StoresInfo> storesInfoPage = page(page, queryWrapper);
        Page<AdminSelectStoresInfoDto> adminSelectStoresInfoDtoPage = PageDtoUtil.convertPage(storesInfoPage, AdminSelectStoresInfoDto.class);
        return adminSelectStoresInfoDtoPage;
    }

    @Override
    public StoresInfo clerkSelectStoresInfo(Integer clerkId) throws Exception {
        //判断是否存在店长账号
        Stores clerk = storesService.getById(clerkId);
        if (clerk == null || clerk.getClerkState() == 2) throw new AccountException(4001,"店长账号不存在");
        //判断店长账号状态
        if (clerk.getClerkState() == 1) throw new AccountException(4002,"店长账号已禁用");
        //查询门店信息
        StoresInfo storesInfo = getById(clerk.getStoresId());
        return storesInfo;
    }

    @Override
    @Transactional
    public String updateStoresInfoImage(Integer clerkId, MultipartFile mf) throws Exception {
        //判断是否存在店长账号
        Stores clerk = storesService.getById(clerkId);
        if (clerk == null || clerk.getClerkState() == 2) throw new AccountException(4001,"店长账号不存在");
        //判断店长账号状态
        if (clerk.getClerkState() == 1) throw new AccountException(4002,"店长账号已禁用");
        //查询门店信息
        StoresInfo storesInfo = getById(clerk.getStoresId());
        //上传图片
        String imageName = aliyunOssUtil.upImage(mf);
        //修改封面
        storesInfo.setStoresImg(imageName);
        return imageName;
    }

    @Override
    @Transactional
    public void updateStoresInfoDetail(Integer clerkId, String storesDetail) throws Exception {
        //判断是否存在店长账号
        Stores clerk = storesService.getById(clerkId);
        if (clerk == null || clerk.getClerkState() == 2) throw new AccountException(4001,"店长账号不存在");
        //判断店长账号状态
        if (clerk.getClerkState() == 1) throw new AccountException(4002,"店长账号已禁用");
        //查询门店信息
        StoresInfo storesInfo = getById(clerk.getStoresId());
        //修改门店详情
        storesInfo.setStoresDetail(storesDetail);
        updateById(storesInfo);
    }

    @Override
    public List<Integer> getAllStoresId(Integer adminId) throws Exception {
        //判断是否存在当前平台账号
        Admin currentAdmin = adminService.getById(adminId);
        if (currentAdmin == null || currentAdmin.getAdminState() == 2)
            throw new AccountException(2001,"平台账号不存在");
        //判断当前平台账号状态
        if (currentAdmin.getAdminState() == 1) throw new AccountException(2002,"平台账号已被禁用");
        //查询所有正常门店
        List<StoresInfo> storesInfos = list(new QueryWrapper<StoresInfo>().eq("stores_state", 0));
        List<Integer> storesInfoIds = storesInfos.stream().map(o -> o.getStoresId()).collect(Collectors.toList());
        return storesInfoIds;
    }

    @Override
    @Transactional
    public void adminDisableStores(Integer adminId, Integer storesId) throws Exception {
        //判断是否存在当前平台账号
        Admin currentAdmin = adminService.getById(adminId);
        if (currentAdmin == null || currentAdmin.getAdminState() == 2)
            throw new AccountException(2001,"平台账号不存在");
        //判断当前平台账号状态
        if (currentAdmin.getAdminState() == 1) throw new AccountException(2002,"平台账号已被禁用");
        //查询门店信息
        StoresInfo storesInfo = getById(storesId);
        //判断门店状态
        if (storesInfo == null) throw new AccountException(2003,"门店信息不存在");
        if (storesInfo.getStoresState() == 1) throw new AccountException(2004,"门店已经是禁用状态");
        //修改门店状态
        storesInfo.setStoresState(1);
        updateById(storesInfo);
    }

    @Override
    public void adminEnableStores(Integer adminId, Integer storesId) throws Exception {
        //判断是否存在当前平台账号
        Admin currentAdmin = adminService.getById(adminId);
        if (currentAdmin == null || currentAdmin.getAdminState() == 2)
            throw new AccountException(2001,"平台账号不存在");
        //判断当前平台账号状态
        if (currentAdmin.getAdminState() == 1) throw new AccountException(2002,"平台账号已被禁用");
        //查询门店信息
        StoresInfo storesInfo = getById(storesId);
        //判断门店状态
        if (storesInfo == null) throw new AccountException(2003,"门店信息不存在");
        if (storesInfo.getStoresState() == 0) throw new AccountException(2004,"门店已经是启用状态");
        //修改门店状态
        storesInfo.setStoresState(0);
        updateById(storesInfo);
    }

    @Override
    public void changeStoresInfoScore(Integer score, Integer storesId) throws Exception {
        //判断门店状态
        StoresInfo storesInfo = getById(storesId);
        if (storesInfo == null || storesInfo.getStoresState() == 1) throw new AccountException(2001,"门店不存在");
        //修改评分
        Double score1 = storesInfo.getScore();
        Integer serveNum = storesInfo.getServeNum();
        double allScore = score1 * serveNum;
        double score2 = (allScore + score) / (serveNum + 1);
        storesInfo.setScore(score2);
        storesInfo.setServeNum(serveNum+1);
        updateById(storesInfo);
    }
}
